package com.rsy.crm.web.listener;

import com.rsy.crm.settings.domain.DicValue;
import com.rsy.crm.settings.service.DicService;
import com.rsy.crm.settings.service.Impl.DicServiceImpl;
import com.rsy.crm.util.ServiceFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

public class SysInitListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("缓存处理数据字典开开始");

        //调用全局作用域
        ServletContext application = sce.getServletContext();

        DicService ds = (DicService) ServiceFactory.getService(new DicServiceImpl());


        Map<String, List<DicValue>> map = ds.getAll();

        Set<String> set = map.keySet();
        for (String key:set){
            System.out.println(key);
            application.setAttribute(key,map.get(key));
        }

        System.out.println("服务器缓存处理数据字典结束");


        /**
         * 处理.properties
         */

        ResourceBundle bundle = ResourceBundle.getBundle("Stage2Possibility");
        Map<String,String> pMap = new HashMap<>();

        Enumeration<String> enumeration = bundle.getKeys();

        while (enumeration.hasMoreElements()){
            //阶段
            String key = enumeration.nextElement();
            //可能性
            String value = bundle.getString(key);

            pMap.put(key,value);
        }
        application.setAttribute("pMap",pMap);


    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
